package com.yanqu.road.server.cmd.activity.summergames;

import com.yanqu.road.entity.activity.summergames.SummerGamesConfig;
import com.yanqu.road.entity.activity.summergames.SummerGamesUserData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SummerGamesProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.summergames.SummerGamesModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.summergames.SummerGamesActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_SUMMER_GAMES_REFUSE_APPLY, desc = "拒绝请求")
@CmdApi(reqMsg = SummerGamesProto.SummerGamesRefuseApplyReqMsg.class, respMsg = SummerGamesProto.SummerGamesRefuseApplyRespMsg.class)
public class SummerGamesRefuseApplyCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SummerGamesConfig config = SummerGamesActivityMgr.getSummerGamesConfig();
        // 没有活动
        if (config == null || !ActivityMgr.activityInTimeAndShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        SummerGamesUserData userData = player.getModule(SummerGamesModule.class).getUserData();
        if(userData == null){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        SummerGamesProto.SummerGamesRefuseApplyReqMsg reqMsg = SummerGamesProto.SummerGamesRefuseApplyReqMsg.parseFrom(packet.getBytes());
        if(reqMsg.getId() < 0){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        SummerGamesProto.CrossSummerGamesRefuseApplyReqMsg.Builder crossMsg = SummerGamesProto.CrossSummerGamesRefuseApplyReqMsg.newBuilder();
        crossMsg.setActivityId(config.getActivityId());
        crossMsg.setId(reqMsg.getId());
        player.sendPacket(Protocol.C_SUMMER_GAMES_REFUSE_APPLY, crossMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_SUMMER_GAMES_REFUSE_APPLY, SummerGamesProto.SummerGamesRefuseApplyRespMsg.newBuilder().setRet(ret));
        }
    }
}
